package com.hs.app.service.impl;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hs.app.dao.IProduceDao;
import com.hs.app.helper.DwrResponseHelper;
import com.hs.app.model.CdcBlfy;
import com.hs.app.model.CdcFjZyZfYlzy;
import com.hs.app.model.CdcHerb;
import com.hs.app.model.CdcJsff;
import com.hs.app.model.CdcLhyy;
import com.hs.app.model.CdcLincdz;
import com.hs.app.model.CdcLinchuang;
import com.hs.app.model.CdcLxzb;
import com.hs.app.model.CdcPrehreb;
import com.hs.app.model.CdcSyqk;
import com.hs.app.service.IProduceService;
import com.hs.core.dao.IBaseDao;
import com.hs.core.model.BaseBean;
import com.hs.core.service.impl.BaseSevice;
import com.hs.core.utils.PageParam;
import com.hs.core.utils.PageUtil;

@Service  
public class ProduceService extends BaseSevice implements IProduceService{
    private IProduceDao produceDao;
    
    @Transactional(readOnly = true)
	public PageUtil getPageList(PageParam pageParam){
		return this.produceDao.getPageList(pageParam);
	}
    
    @Transactional
    public void deleteByProperties(Class<?> clazz, String[] params, String[] values) {
		produceDao.deleteBean(clazz, params, values);
	}

	@Transactional
	public int saveOrUpdate(BaseBean bean) {
		try {
			Class<?> clazz = bean.getClass();
			Method method = clazz.getMethod("getId", null);
			Long id = (Long)method.invoke(bean);
			if(null == id){
				produceDao.save(bean);
				return 1;
			}else{
				produceDao.update(bean);
				return 2;
			}
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		return 0;
	}
	
	@Transactional
	public void deleteCascadeTable(String formId,String selfId,String refrenceId){
		if("syqk".equals(formId)){ //删试验情况
			deleteByProperties(CdcSyqk.class, new String[]{"id"}, new String[]{selfId});
			deleteByProperties(CdcJsff.class, new String[]{"pid","syid"}, new String[]{refrenceId,selfId});
			deleteByProperties(CdcFjZyZfYlzy.class, new String[]{"pid","syid"}, new String[]{refrenceId,selfId});
			deleteByProperties(CdcLxzb.class, new String[]{"pid","rid","rtype"}, new String[]{refrenceId,selfId,"1"});
		}else if("zycy".equals(formId)){//删中药成药
			deleteByProperties(CdcPrehreb.class, new String[]{"id"}, new String[]{selfId});
			deleteByProperties(CdcHerb.class, new String[]{"pid","zycyid"}, new String[]{refrenceId,selfId});
		}else if("linchuang".equals(formId)){//删临床观察
			deleteByProperties(CdcLinchuang.class, new String[]{"id"}, new String[]{selfId});
			deleteByProperties(CdcLhyy.class, new String[]{"pid","lcid"}, new String[]{refrenceId,selfId});
			deleteByProperties(CdcBlfy.class, new String[]{"pid","lcid"}, new String[]{refrenceId,selfId});
			deleteByProperties(CdcLxzb.class, new String[]{"pid","rid","rtype"}, new String[]{refrenceId,selfId,"2"});
			deleteByProperties(CdcLincdz.class, new String[]{"pid","lcid"}, new String[]{refrenceId,selfId});
		}else{
			Class<?> clazz = DwrResponseHelper.getObjectClazz(formId);
			deleteByProperties(clazz, new String[]{"id"}, new String[]{selfId}); //单表删除
		}
	}
	
	@Transactional
	public List<Object> findObjectList(Class<?> clazz,String[] params,String[] values){
		return produceDao.findBeanList(clazz, params, values);
	}

	@Override
	public IBaseDao getBaseDao() {
		return produceDao;
	}
	@Autowired
	public void setSearchDao(IProduceDao ProduceDao) {
		this.produceDao = ProduceDao;
	}
	public IProduceDao getProduceDao() {
		return produceDao;
	}
	public void setProduceDao(IProduceDao produceDao) {
		this.produceDao = produceDao;
	}
}
